package com.example.controller;

import com.aliyuncs.exceptions.ClientException;
import com.example.pojo.Result;
import com.example.utils.AliOSSUtils;
import jakarta.servlet.annotation.MultipartConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliOSSUtils aliOSSUtils;
//    @PostMapping("/upload")
//    public Result upload(String name, Integer age, MultipartFile image) {
//        log.info("文件上传 name:{},age:{},image{}", name, age, image);
//        //获取原始文件名-1.jpg
//        String originalFilename = image.getOriginalFilename();
//        String suffix = "";
//        log.info("原始文件名:{}", originalFilename);
//        if (originalFilename != null) {
//             suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
//        }
//        //构造唯一的文件名(不能重复)——uuid(通用唯一识别码) 78e07877-c007-4e50-861c-72b24a46fe30
//        String newFilename = UUID.randomUUID().toString()+suffix;
//        log.info("文件名:{}", newFilename);
//
//        //将文件保存到服务器的磁盘中 E:\accept_files
//        try {
//            image.transferTo(new File("E:\\accept_files\\" + newFilename));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//
//        return Result.success();
//    }
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException, ClientException {
        log.info("文件上传:{}", image.getOriginalFilename());
//        使用oss工具类进行文件上传
        String url=aliOSSUtils.upload(image);
        return Result.success(url);
    }

}
